﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Text;
using web.bussines;
using web.data.entities;
using web.core.Models;
using web.core.ViewModels;
using web.component;

namespace web.core.Controllers
{
    public class homeController : baseController
    {
        public ActionResult index()
        {
            return PartialView("../home/index");
        }

        /// <summary>
        /// Get metadata for product list
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult home_metadata()
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            var metadata_view = new ViewModels.MetadataViewModels();

            metadata_view.title = Uconfig.GetConfig(global_const.KEYWORD, global_const.LANGUAGE_CODE_DEFAULT);
            metadata_view.keyword = Uconfig.GetConfig(global_const.KEYWORD, global_const.LANGUAGE_CODE_DEFAULT);
            metadata_view.description = Uconfig.GetConfig(global_const.DESCRIPTION, global_const.LANGUAGE_CODE_DEFAULT);
           
            base_metadata(metadata_view);
            return PartialView("../Shared/commont/metadata");
        }

        #region sitemap
        public ActionResult sitemap()
        {
            return PartialView("../home/sitemap");
        }
        #endregion

        #region contact page
        public ActionResult contact()
        {
            return PartialView("../home/contact");
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult control_contact()
        {
            var language_code = DefaultLanguageGetter();
            var contact_view = new ViewModels.ContactClientViewModels() {
                contact_content_text =  Uxmlhelper.GetTranslate(global_const.E_TRANS_GROUP.PAGE_LAYOUT, global_const.CONTACTDETAIL, language_code)
            };
            return PartialView("../Shared/home/control_contact", contact_view);
        }

        [ChildActionOnly]
        [HttpPost]
        public ActionResult control_contact(FormCollection values)
        {
            var contact_view = new ViewModels.ContactClientViewModels();
            TryUpdateModel(contact_view);

            //valid form in server code
            if (string.IsNullOrEmpty(contact_view.contact_name))
            {
                ViewBag.result_message = "Quý khách vui lòng điền Họ và Tên.";
                ViewBag.result = false;
                return PartialView("../Shared/home/control_contact", contact_view);
            }

            if (string.IsNullOrEmpty(contact_view.contact_email))
            {
                ViewBag.result_message = "Quý khách vui lòng nhập email.";
                ViewBag.result = false;
                return PartialView("../Shared/home/control_contact", contact_view);
            }

            if (string.IsNullOrEmpty(contact_view.content_full))
            {
                ViewBag.result_message = "Quý khách vui lòng nhập nội dung.";
                ViewBag.result = false;
                return PartialView("../Shared/home/control_contact", contact_view);
            }

            //check captcha text
            if (HttpContext.Session["captchastring"] != null && contact_view.captcha_text != HttpContext.Session["captchastring"].ToString())
            {
                ViewBag.result_message = "Quý khách vui lòng nhập lại mã xác nhận.";
                ViewBag.result = false;
                return PartialView("../Shared/home/control_contact", contact_view);
            }

            var client_contact = new data.entities.client_contact()
            { 
               contact_name = contact_view.contact_name,
               contact_email = contact_view.contact_email,
               contact_phone = contact_view.contact_phone,
               contact_address = contact_view.contact_address,
               content_full = contact_view.content_full,
               is_read= false,
               title = contact_view.title, 
               sent_date = Udate_time.GetCurrentDateTime(),
            };
                       
            var new_id = clientContactModels.update(client_contact);

            if (new_id > 0)
            {
                //gui email to staff
                var mail_id = global_const.EMAIL_ID_CLIENT_CONTACT;
                string lang_code = DefaultLanguageGetter();
                var lst_content = new List<EmailVariable>()
                                  {
                                      new EmailVariable("email_subject", contact_view.title),
                                      new EmailVariable("client_name", contact_view.contact_name),
                                      new EmailVariable("client_email", contact_view.contact_email),
                                      new EmailVariable("client_phone", contact_view.contact_phone),
                                      new EmailVariable("email_subject", contact_view.title),
                                      new EmailVariable("client_address", contact_view.contact_address),
                                      new EmailVariable("comment", contact_view.content_full),
                                  };
                var email_admin = Uconfig.GetConfig(global_const.EMAIL, lang_code);
                bool send_email_admin = false;
                if (!string.IsNullOrEmpty(email_admin))
                    send_email_admin = SendWebEmail(mail_id, lst_content, email_admin, "", "");


                ViewBag.result_message = "Yêu cầu của bạn đã được gửi.";
                ViewBag.result = true;
                return PartialView("../Shared/home/control_contact", contact_view);
            }
            else
            {
                ViewBag.result_message = "Yêu cầu của bạn đã không được gửi.";
                ViewBag.result = false;
                return PartialView("../Shared/home/control_contact", contact_view);
            }
        }


        /// <summary>
        /// Get metadata for product list
        /// </summary>
        /// <param name="cateId"></param>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult contact_metadata()
        {
            int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            var metadata_view = new ViewModels.MetadataViewModels();

            metadata_view.title = Uxmlhelper.GetTranslate(global_const.E_TRANS_GROUP.PAGE_LAYOUT, global_const.CONTACT, global_const.LANGUAGE_CODE_DEFAULT);
            metadata_view.keyword = Uconfig.GetConfig(global_const.KEYWORD, global_const.LANGUAGE_CODE_DEFAULT);
            metadata_view.description = Uconfig.GetConfig(global_const.DESCRIPTION, global_const.LANGUAGE_CODE_DEFAULT);

            base_metadata(metadata_view);
            return PartialView("../Shared/commont/metadata");
        }
        #endregion
    }
}
